package admin.servlet;

import admin.dao.Addao;
import admin.entity.Adenty;
import admin.utils.JBDCUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.SQLException;

@WebServlet("/select")
public class selservlete extends HttpServlet {
    private static final long serialVersionUID = 1L;
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String incid = req.getParameter("incid");
        System.out.println("查询的账号:"+incid);

        resp.setContentType("text/html;charset=UTF-8");
        PrintWriter out = resp.getWriter();
        JBDCUtil db = new JBDCUtil();

        // 创建一个用户保存账号
        Adenty adenty=new Adenty();
        adenty.setCid(incid);
        Addao dao = new Addao();

        try {
            Connection conn = db.getConn();
            if (dao.select(conn,adenty) != null) {
                System.out.println(adenty);
                req.setAttribute("adenty",dao.select(conn,adenty));
                req.getRequestDispatcher("seladres.jsp").forward(req,resp);
            }else {
                out.println("<h1>账号不存在!</h1>");
                resp.setHeader("Refresh", "1;url=seladinfo.jsp");
            }
        } catch (SQLException exception) {
            exception.printStackTrace();
        }finally {
            out.close();
        }
    }
}
